package com.juyu.miliao.api.dao.redis;

import com.juyu.miliao.api.cache.RedisKeyPrefix;
import com.juyu.miliao.api.util.RedisKeys;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Repository;

import java.awt.event.KeyEvent;
import java.util.Optional;
import java.util.concurrent.TimeUnit;

/**
 * @author xuanqicheng
 * @create 2019-17-11:30
 */
@Repository
public class UserAuthenticationInfoRedisDao {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 将手机验证码存入redis
     * @param phone 手机号
     * @param validateCode 随机验证码
     * @param minutes 保存时间，单位：分钟
     */
    public void sendPhoneNumVerificationCode(String phone, String validateCode, int minutes) {
        String key = RedisKeys.USER_AUTHENTICATION_PHONE_VALIDATE_CODE.getValue(phone);
        stringRedisTemplate.opsForValue().set(key,validateCode,minutes, TimeUnit.MINUTES);
    }

    public String getPhoneNumVerificationCode(String phone) {

        String key = RedisKeys.USER_AUTHENTICATION_PHONE_VALIDATE_CODE.getValue(phone);

        String code = stringRedisTemplate.opsForValue().get(key);

        return code;
    }

    public Optional<Integer> getUserIdByUserName(String username){
        Object o = stringRedisTemplate.opsForHash().get("username", username);
        Integer userId = null;
        if (o != null) {
            userId = Integer.parseInt(o.toString());
        }
        return Optional.ofNullable(userId);
    }
    public void addUserName(String username, long userId){
        stringRedisTemplate.opsForHash().put("username", username, userId + "");
    }

    public boolean isMemberNewUserBinding(long userId){
        Boolean temp = stringRedisTemplate.opsForSet().isMember("newUserBindingPhoneGiftDeposit",userId+"");
        if(temp == null){
            return false;
        }
        return temp;
    }
    public void removeNewUserBinding(long userId){
        stringRedisTemplate.opsForSet().remove("newUserBindingPhoneGiftDeposit",userId+"");
    }
}
